package it.nola.soccer;

import android.app.Activity;
import android.database.Cursor;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;

public class TeamEdit extends Activity {
    private DbAdapter mDbHelper;
    short squadra;
    long cronaca;
    String nomeSquadra="";
    EditText[] nrText=new EditText[DbConst.players.MAX_NUMBER];
    EditText[] nomiText=new EditText[DbConst.players.MAX_NUMBER];
    EditText[] idText=new EditText[DbConst.players.MAX_NUMBER];

    private void fillData() {
	Cursor tc=mDbHelper.cercaSquadra(squadra, cronaca);
	if (!tc.moveToFirst()) { // se è vuoto
	    tc=mDbHelper.riempiSqadra(squadra, cronaca);
	}
	int i=0;
	do {
	    nrText[i].setText(tc.getString(tc.getColumnIndex(DbConst.players.KEY_NR)));
	    nomiText[i].setText(tc.getString(tc.getColumnIndex(DbConst.players.KEY_NAME)));
	    idText[i].setText(tc.getString(tc.getColumnIndex(DbConst.players.KEY_ROWID)));
	    i++;
	}
	while (tc.move(1));
	tc.close();
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
	super.onCreate(savedInstanceState);
	// recupera i parametri passati tramite Intent
	Bundle extras=getIntent().getExtras();
	if (extras!=null) {
	    cronaca=extras.getLong(DbConst.players.KEY_CRONACA);
	    squadra=extras.getShort(DbConst.players.KEY_TEAM);
	    nomeSquadra=extras.getString(DbConst.players.NAME);
	}
	setTitle(nomeSquadra);
	setContentView(R.layout.team_edit);
	prepareFields();
	mDbHelper=new DbAdapter(this);
	mDbHelper.open();
	fillData();
    }

    @Override
    protected void onPause() {
	super.onPause();
	saveState();
    }

    @Override
    protected void onStop() {
	super.onPause();
	saveState();
	mDbHelper.close();
    }

    /**
     * Inizializza i campi presenti nella View
     */
    private void prepareFields() {
	nrText[0]=(EditText) findViewById(R.id.nr1);
	nrText[1]=(EditText) findViewById(R.id.nr2);
	nrText[2]=(EditText) findViewById(R.id.nr3);
	nrText[3]=(EditText) findViewById(R.id.nr4);
	nrText[4]=(EditText) findViewById(R.id.nr5);
	nrText[5]=(EditText) findViewById(R.id.nr6);
	nrText[6]=(EditText) findViewById(R.id.nr7);
	nrText[7]=(EditText) findViewById(R.id.nr8);
	nrText[8]=(EditText) findViewById(R.id.nr9);
	nrText[9]=(EditText) findViewById(R.id.nr10);
	nrText[10]=(EditText) findViewById(R.id.nr11);
	nrText[11]=(EditText) findViewById(R.id.nr12);
	nrText[12]=(EditText) findViewById(R.id.nr13);
	nrText[13]=(EditText) findViewById(R.id.nr14);
	nrText[14]=(EditText) findViewById(R.id.nr15);
	nrText[15]=(EditText) findViewById(R.id.nr16);
	nrText[16]=(EditText) findViewById(R.id.nr17);
	nrText[17]=(EditText) findViewById(R.id.nr18);
	nrText[18]=(EditText) findViewById(R.id.nrAll);
	nomiText[0]=(EditText) findViewById(R.id.name1);
	nomiText[1]=(EditText) findViewById(R.id.name2);
	nomiText[2]=(EditText) findViewById(R.id.name3);
	nomiText[3]=(EditText) findViewById(R.id.name4);
	nomiText[4]=(EditText) findViewById(R.id.name5);
	nomiText[5]=(EditText) findViewById(R.id.name6);
	nomiText[6]=(EditText) findViewById(R.id.name7);
	nomiText[7]=(EditText) findViewById(R.id.name8);
	nomiText[8]=(EditText) findViewById(R.id.name9);
	nomiText[9]=(EditText) findViewById(R.id.name10);
	nomiText[10]=(EditText) findViewById(R.id.name11);
	nomiText[11]=(EditText) findViewById(R.id.name12);
	nomiText[12]=(EditText) findViewById(R.id.name13);
	nomiText[13]=(EditText) findViewById(R.id.name14);
	nomiText[14]=(EditText) findViewById(R.id.name15);
	nomiText[15]=(EditText) findViewById(R.id.name16);
	nomiText[16]=(EditText) findViewById(R.id.name17);
	nomiText[17]=(EditText) findViewById(R.id.name18);
	nomiText[18]=(EditText) findViewById(R.id.nameAll);
	idText[0]=(EditText) findViewById(R.id.player1_id);
	idText[1]=(EditText) findViewById(R.id.player2_id);
	idText[2]=(EditText) findViewById(R.id.player3_id);
	idText[3]=(EditText) findViewById(R.id.player4_id);
	idText[4]=(EditText) findViewById(R.id.player5_id);
	idText[5]=(EditText) findViewById(R.id.player6_id);
	idText[6]=(EditText) findViewById(R.id.player7_id);
	idText[7]=(EditText) findViewById(R.id.player8_id);
	idText[8]=(EditText) findViewById(R.id.player9_id);
	idText[9]=(EditText) findViewById(R.id.player10_id);
	idText[10]=(EditText) findViewById(R.id.player11_id);
	idText[11]=(EditText) findViewById(R.id.player12_id);
	idText[12]=(EditText) findViewById(R.id.player13_id);
	idText[13]=(EditText) findViewById(R.id.player14_id);
	idText[14]=(EditText) findViewById(R.id.player15_id);
	idText[15]=(EditText) findViewById(R.id.player16_id);
	idText[16]=(EditText) findViewById(R.id.player17_id);
	idText[17]=(EditText) findViewById(R.id.player18_id);
	idText[18]=(EditText) findViewById(R.id.playerAll_id);
    }

    public void salva(View view) {
	setResult(RESULT_OK);
	finish();
    }

    private void saveState() {
	for (int i=0; i<nrText.length; i++) {
	    mDbHelper.updatePlayer(Long.parseLong(idText[i].getText().toString()), squadra, cronaca, nrText[i]
		.getText().toString(), nomiText[i].getText().toString());
	}
    }
}
